package lily.phs.pregnance;

import lily.phs.object.FirstPrenatalVisit;
import lily.phs.object.HereTipa;
import lily.phs.urh.R;
import lily.ui.control.MyPopupWindow;


import android.content.Context;
import android.text.InputType;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

/**
 * 第1次产前随访记录表（第1页，共2页）的节目界面布局和数据处理
 * @author heckassitant
 *
 */
public class FirstPrenatalVisitView extends LinearLayout implements OnClickListener, OnFocusChangeListener{
	
	private MyPopupWindow mPopupWindow;
	private Context mContext;
	private int profileid=0;
	
	private EditText et_name,et_id,et_date,et_preg_week,et_age,et_husband_name,et_husband_age,et_husband_phone,et_gravidity,
	                 et_vaginal_delivery_times,et_cesarean_section_times,et_last_menses_date,et_ycq,et_past_history_other,et_family_other,
	                 et_personal_other,et_gyneopration,et_miscarry,et_dying_fetus,et_stillbirth,et_neonatal_death,et_birth_defect,
	                 et_height,et_weight,et_bmt,et_bloodpressure_high,et_bloodpressure_low,et_heart_abnormal,et_lung_abnormal,
	                 et_vulva_abnormal,et_vagina_abnormal,et_cervix_abnormal,et_uterus_abnormal,et_appendix_abnormal;
	private RadioGroup rgGyneopration,rgHasHeartAbnormal,rgHasLungAbnormal,rgHasVulvaAbnormal,rgHasVaginaAbnormal,rgHasCervixAbnormal,rgHasUterusAbnormal,rgHasAppendixAbnormal;
	private RadioButton rb_last_menses_1,rb_last_menses_2,rb_gyneopration_1,rb_gyneopration_2,rb_has_heart_abnormal_no,rb_has_heart_abnormal_yes,
	                    rb_has_lung_abnormal_no,rb_has_lung_abnormal_yes,rb_has_vulva_abnormal_no,rb_has_vulva_abnormal_yes,
	                    rb_has_vagina_abnormal_no,rb_has_vagina_abnormal_yes,rb_has_cervix_abnormal_no,rb_has_cervix_abnormal_yes,
	                    rb_has_uterus_abnormal_no,rb_has_uterus_abnormal_yes,rb_has_appendix_abnormal_no,rb_has_appendix_abnormal_yes;
	
	private CheckBox cb_past_history_1,cb_past_history_2,cb_past_history_3,cb_past_history_4,cb_past_history_5,cb_past_history_6,
	                 cb_past_history_7,cb_past_history_8,cb_family_1,cb_family_2,cb_family_3,cb_personal_1,cb_personal_2,cb_personal_3,
	                 cb_personal_4,cb_personal_5,cb_personal_6,cb_pregnance_history_1,cb_pregnance_history_2,cb_pregnance_history_3,
	                 cb_pregnance_history_4,cb_pregnance_history_5;
	
	private HereTipa ht;
	private FirstPrenatalVisit fpv;
	private View child;
	
	public FirstPrenatalVisitView(Context context,int profileId) {
		super(context);
		// TODO Auto-generated constructor stub
		this.profileid = profileId;
		this.mContext = context;
		final LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		child = inflater.inflate(R.layout.tablepreg1, null);
		
		//这是第一个页面，在主线程中加载，但数据处理可放在次线程中
		findViewsById();
		setMyView();
	}
	
	/**
	 * 将整个界面作为一个view对象
	 * @return 整个界面view
	 */
	public View getMyView(){
		return this.child;
	}
	
	/**
	 * 控件与xml文件关联
	 */
	public void findViewsById() {
		// TODO Auto-generated method stub
		et_name = (EditText)child.findViewById(R.id.first_name);
		et_id = (EditText)child.findViewById(R.id.first_id);
		et_date = (EditText)child.findViewById(R.id.first_date);
		et_date.setInputType(InputType.TYPE_NULL);//不显示虚拟键盘
		et_date.setOnClickListener(this);
		et_date.setOnFocusChangeListener(this);
		et_preg_week = (EditText)child.findViewById(R.id.first_preg_week);
		et_age = (EditText)child.findViewById(R.id.first_age);
		et_husband_name = (EditText)child.findViewById(R.id.first_husband_name);
		et_husband_age = (EditText)child.findViewById(R.id.first_husband_age);
		et_husband_phone = (EditText)child.findViewById(R.id.first_husband_phone);
		et_gravidity = (EditText)child.findViewById(R.id.first_gravidity);
        et_vaginal_delivery_times = (EditText)child.findViewById(R.id.first_vaginal_delivery_times);
        et_cesarean_section_times = (EditText)child.findViewById(R.id.first_cesarean_section_times);
        et_last_menses_date = (EditText)child.findViewById(R.id.first_last_menses_date);
        et_last_menses_date.setInputType(InputType.TYPE_NULL);//不显示虚拟键盘
        et_last_menses_date.setOnClickListener(this);
        et_ycq = (EditText)child.findViewById(R.id.first_ycq);
        et_past_history_other = (EditText)child.findViewById(R.id.first_past_history_other);
        et_past_history_other.setOnFocusChangeListener(this);
        et_past_history_other.setOnClickListener(this);
        et_family_other = (EditText)child.findViewById(R.id.first_family_other);
        et_family_other.setOnFocusChangeListener(this);
        et_family_other.setOnClickListener(this);
        et_personal_other = (EditText)child.findViewById(R.id.first_personal_other);
        et_personal_other.setOnFocusChangeListener(this);
        et_personal_other.setOnClickListener(this);
        et_gyneopration = (EditText)child.findViewById(R.id.first_gyneopration);
        et_gyneopration.setOnFocusChangeListener(this);
        et_gyneopration.setOnClickListener(this);
        et_miscarry = (EditText)child.findViewById(R.id.first_miscarry);
        et_miscarry.setOnFocusChangeListener(this);
        et_miscarry.setOnClickListener(this);
        et_dying_fetus = (EditText)child.findViewById(R.id.first_dying_fetus);
        et_dying_fetus.setOnFocusChangeListener(this);
        et_dying_fetus.setOnClickListener(this);
        et_stillbirth = (EditText)child.findViewById(R.id.first_stillbirth);
        et_stillbirth.setOnFocusChangeListener(this);
        et_stillbirth.setOnClickListener(this);
        et_neonatal_death = (EditText)child.findViewById(R.id.first_neonatal_death);
        et_neonatal_death.setOnFocusChangeListener(this);
        et_neonatal_death.setOnClickListener(this);
        et_birth_defect = (EditText)child.findViewById(R.id.first_birth_defect);
        et_birth_defect.setOnFocusChangeListener(this);
        et_birth_defect.setOnClickListener(this);
        et_height = (EditText)child.findViewById(R.id.first_height);
        et_weight = (EditText)child.findViewById(R.id.first_weight);
        et_bmt = (EditText)child.findViewById(R.id.first_bmt);
        et_bloodpressure_high = (EditText)child.findViewById(R.id.first_bloodpressure_high);
        et_bloodpressure_low = (EditText)child.findViewById(R.id.first_bloodpressure_low);
        et_heart_abnormal = (EditText)child.findViewById(R.id.first_heart_abnormal);
        et_heart_abnormal.setOnFocusChangeListener(this);
        et_heart_abnormal.setOnClickListener(this);
        et_lung_abnormal = (EditText)child.findViewById(R.id.first_lung_abnormal);
        et_lung_abnormal.setOnFocusChangeListener(this);
        et_lung_abnormal.setOnClickListener(this);
        et_vulva_abnormal = (EditText)child.findViewById(R.id.first_vulva_abnormal);
        et_vulva_abnormal.setOnFocusChangeListener(this);
        et_vulva_abnormal.setOnClickListener(this);
        et_vagina_abnormal = (EditText)child.findViewById(R.id.first_vagina_abnormal);
        et_vagina_abnormal.setOnFocusChangeListener(this);
        et_vagina_abnormal.setOnClickListener(this);
        et_cervix_abnormal = (EditText)child.findViewById(R.id.first_cervix_abnormal);
        et_cervix_abnormal.setOnFocusChangeListener(this);
        et_cervix_abnormal.setOnClickListener(this);
        et_uterus_abnormal = (EditText)child.findViewById(R.id.first_uterus_abnormal);
        et_uterus_abnormal.setOnFocusChangeListener(this);
        et_uterus_abnormal.setOnClickListener(this);
        et_appendix_abnormal = (EditText)child.findViewById(R.id.first_appendix_abnormal);
        et_appendix_abnormal.setOnFocusChangeListener(this);
        et_appendix_abnormal.setOnClickListener(this);
        rb_last_menses_1 = (RadioButton)child.findViewById(R.id.first_last_menses_1);
		rb_last_menses_1.setOnClickListener(this);
		rb_last_menses_2 = (RadioButton)child.findViewById(R.id.first_last_menses_2);
		rb_last_menses_2.setOnClickListener(this);
		rb_gyneopration_1 = (RadioButton)child.findViewById(R.id.first_gyneopration_1);
		rb_gyneopration_2 = (RadioButton)child.findViewById(R.id.first_gyneopration_2);
		rb_has_heart_abnormal_no = (RadioButton)child.findViewById(R.id.first_has_heart_abnormal_no);
		rb_has_heart_abnormal_yes = (RadioButton)child.findViewById(R.id.first_has_heart_abnormal_yes);
        rb_has_lung_abnormal_no = (RadioButton)child.findViewById(R.id.first_has_lung_abnormal_no);
        rb_has_lung_abnormal_yes = (RadioButton)child.findViewById(R.id.first_has_lung_abnormal_yes);
        rb_has_vulva_abnormal_no = (RadioButton)child.findViewById(R.id.first_has_vulva_abnormal_no);
        rb_has_vulva_abnormal_yes = (RadioButton)child.findViewById(R.id.first_has_vulva_abnormal_yes);
        rb_has_vagina_abnormal_no = (RadioButton)child.findViewById(R.id.first_has_vagina_abnormal_no);
        rb_has_vagina_abnormal_yes = (RadioButton)child.findViewById(R.id.first_has_vagina_abnormal_yes);
        rb_has_cervix_abnormal_no = (RadioButton)child.findViewById(R.id.first_has_cervix_abnormal_no);
        rb_has_cervix_abnormal_yes = (RadioButton)child.findViewById(R.id.first_has_cervix_abnormal_yes);
        rb_has_uterus_abnormal_no = (RadioButton)child.findViewById(R.id.first_has_uterus_abnormal_no);
        rb_has_uterus_abnormal_yes = (RadioButton)child.findViewById(R.id.first_has_uterus_abnormal_yes);
        rb_has_appendix_abnormal_no = (RadioButton)child.findViewById(R.id.first_has_appendix_abnormal_no);
        rb_has_appendix_abnormal_yes = (RadioButton)child.findViewById(R.id.first_has_appendix_abnormal_yes);
		cb_past_history_1 = (CheckBox)child.findViewById(R.id.first_past_history_1);
		cb_past_history_2 = (CheckBox)child.findViewById(R.id.first_past_history_2);
		cb_past_history_3 = (CheckBox)child.findViewById(R.id.first_past_history_3);
		cb_past_history_4 = (CheckBox)child.findViewById(R.id.first_past_history_4);
		cb_past_history_5 = (CheckBox)child.findViewById(R.id.first_past_history_5);
		cb_past_history_6 = (CheckBox)child.findViewById(R.id.first_past_history_6);
        cb_past_history_7 = (CheckBox)child.findViewById(R.id.first_past_history_7);
        cb_past_history_8 = (CheckBox)child.findViewById(R.id.first_past_history_8);
        cb_family_1 = (CheckBox)child.findViewById(R.id.first_family_1);
        cb_family_2 = (CheckBox)child.findViewById(R.id.first_family_2);
        cb_family_3 = (CheckBox)child.findViewById(R.id.first_family_3);
        cb_personal_1 = (CheckBox)child.findViewById(R.id.first_personal_1);
        cb_personal_2 = (CheckBox)child.findViewById(R.id.first_personal_2);
        cb_personal_3 = (CheckBox)child.findViewById(R.id.first_personal_3);
        cb_personal_4 = (CheckBox)child.findViewById(R.id.first_personal_4);
        cb_personal_5 = (CheckBox)child.findViewById(R.id.first_personal_5);
        cb_personal_6 = (CheckBox)child.findViewById(R.id.first_personal_6);
        cb_pregnance_history_1 = (CheckBox)child.findViewById(R.id.first_pregnance_history_1);
        cb_pregnance_history_2 = (CheckBox)child.findViewById(R.id.first_pregnance_history_2);
        cb_pregnance_history_3 = (CheckBox)child.findViewById(R.id.first_pregnance_history_3);
        cb_pregnance_history_4 = (CheckBox)child.findViewById(R.id.first_pregnance_history_4);
        cb_pregnance_history_5 = (CheckBox)child.findViewById(R.id.first_pregnance_history_5);
        rgGyneopration = (RadioGroup)child.findViewById(R.id.RadioGroup08);
        rgHasHeartAbnormal = (RadioGroup)child.findViewById(R.id.RadioGroup01);
        rgHasLungAbnormal = (RadioGroup)child.findViewById(R.id.RadioGroup02);
        rgHasVulvaAbnormal = (RadioGroup)child.findViewById(R.id.RadioGroup05);
        rgHasVaginaAbnormal = (RadioGroup)child.findViewById(R.id.RadioGroup06);
        rgHasCervixAbnormal = (RadioGroup)child.findViewById(R.id.RadioGroup04);
        rgHasUterusAbnormal = (RadioGroup)child.findViewById(R.id.RadioGroup07);
        rgHasAppendixAbnormal = (RadioGroup)child.findViewById(R.id.RadioGroup03);
	}

	/**
	 * 初始化views
	 */
	public void initViews() {
		// TODO Auto-generated method stub
		initialState();
		
		String temp = "";
		int itemp = -1;
		float ftemp = -1f;
		
		temp = ht.getHeti_name();
		et_name.setText(temp);
		et_name.setEnabled(false);
		temp = ht.getHeti_here_id();
		et_id.setText(temp);
		et_id.setEnabled(false);
		
		temp = fpv.getFipv_date();
		et_date.setText(temp);
		itemp = fpv.getFipv_preg_week();
		if(itemp!=-1){
			et_preg_week.setText(String.valueOf(itemp));			
		}
		itemp = fpv.getFipv_age();
		if(itemp!=-1){
			et_age.setText(String.valueOf(itemp));
		}
		temp = fpv.getFipv_husband_name();
		et_husband_name.setText(temp);
		itemp = fpv.getFipv_husband_age();
		if(itemp!=-1){
			et_husband_age.setText(String.valueOf(itemp));
		}
		temp = fpv.getFipv_husband_phone();
		et_husband_phone.setText(temp);
		
		itemp = fpv.getFipv_gravidity();
		if(itemp!=-1){
			et_gravidity.setText(String.valueOf(itemp));
		}
		itemp = fpv.getFipv_vaginal_delivery_times();
		if(itemp!=-1){
			et_vaginal_delivery_times.setText(String.valueOf(itemp));
		}
		itemp = fpv.getFipv_cesarean_section_times();
		if(itemp!=-1){
			et_cesarean_section_times.setText(String.valueOf(itemp));
		}
		itemp = fpv.getFipv_last_menses();
		switch(itemp){
		case 1:{
			rb_last_menses_1.setChecked(true);
			temp = fpv.getFipv_last_menses_date();
			et_last_menses_date.setText(temp);
			break;
		}
		case 2:
			rb_last_menses_2.setChecked(true);
			break;
		}
		temp = fpv.getFipv_ycq();
		et_ycq.setText(temp);
		
		temp = fpv.getFipv_past_history();
		if(temp.length()>0){
			String[] ss = temp.split(",");
			for(int i=0;i<ss.length;i++){
				int k = Integer.parseInt(ss[i]);
				switch(k){
				case 1:
					cb_past_history_1.setChecked(true);
					break;
				case 2:
					cb_past_history_2.setChecked(true);
					break;
				case 3:
					cb_past_history_3.setChecked(true);
					break;
				case 4:
					cb_past_history_4.setChecked(true);
					break;
				case 5:
					cb_past_history_5.setChecked(true);
					break;
				case 6:
					cb_past_history_6.setChecked(true);
					break;
				case 7:
					cb_past_history_7.setChecked(true);
					break;
				case 8:{
					cb_past_history_8.setChecked(true);
					temp = fpv.getFipv_past_history_other();
					et_past_history_other.setText(temp);
					break;
				}
				}
			}
		}
		
		temp = fpv.getFipv_family();
		if(temp.length()>0){
			String[] ss = temp.split(",");
			for(int i=0;i<ss.length;i++){
				int k = Integer.parseInt(ss[i]);
				switch(k){
				case 1:
					cb_family_1.setChecked(true);
					break;
				case 2:
					cb_family_2.setChecked(true);
					break;
				case 3:{
					cb_family_3.setChecked(true);
					temp = fpv.getFipv_family_other();
					et_family_other.setText(temp);
					break;
				}
				}
			}
		}
		
		temp = fpv.getFipv_personal();
		if(temp.length()>0){
			String[] ss = temp.split(",");
			for(int i=0;i<ss.length;i++){
				int k = Integer.parseInt(ss[i]);
				switch(k){
				case 1:
					cb_personal_1.setChecked(true);
					break;
				case 2:
					cb_personal_2.setChecked(true);
					break;
				case 3:
					cb_personal_3.setChecked(true);
					break;
				case 4:
					cb_personal_4.setChecked(true);
					break;
				case 5:
					cb_personal_5.setChecked(true);
					break;
				case 6:{
					cb_personal_6.setChecked(true);
					temp = fpv.getFipv_personal_other();
					et_personal_other.setText(temp);
					break;
				}
				}
			}
		}
		
		itemp = fpv.getFipv_has_gyneopration();
		switch(itemp){
		case 1:
			rgGyneopration.check(0);
			rb_gyneopration_1.setChecked(true);
			break;
		case 2:{
			rgGyneopration.check(1);
			rb_gyneopration_2.setChecked(true);
			temp = fpv.getFipv_gyneopration();
			et_gyneopration.setText(temp);
			break;
		}
		}
		
		temp = fpv.getFipv_pregnance_history();
		if(temp.length()>0){
			String[] ss = temp.split(",");
			for(int i=0;i<ss.length;i++){
				int k = Integer.parseInt(ss[i]);
				switch(k){
				case 1:{
					cb_pregnance_history_1.setChecked(true);
					itemp = fpv.getFipv_miscarry();
					if(itemp!=-1){
						et_miscarry.setText(String.valueOf(itemp));
					}
					break;
				}
				case 2:{
					cb_pregnance_history_2.setChecked(true);
					itemp = fpv.getFipv_dying_fetus();
					if(itemp!=-1){
						et_dying_fetus.setText(String.valueOf(itemp));
					}
					break;
				}
				case 3:{
					cb_pregnance_history_3.setChecked(true);
					itemp = fpv.getFipv_stillbirth();
					if(itemp!=-1){
						et_stillbirth.setText(String.valueOf(itemp));
					}
					break;
				}
				case 4:{
					cb_pregnance_history_4.setChecked(true);
					itemp = fpv.getFipv_neonatal_death();
					if(itemp!=-1){
						et_neonatal_death.setText(String.valueOf(itemp));
					}
					break;
				}
				case 5:{
					cb_pregnance_history_5.setChecked(true);
					itemp = fpv.getFipv_birth_defect();
					if(itemp!=-1){
						et_birth_defect.setText(String.valueOf(itemp));
					}
					break;
				}
				}
			}
		}
		
		itemp = fpv.getFipv_height();
		if(itemp!=-1){
			et_height.setText(String.valueOf(itemp));
		}
		ftemp = fpv.getFipv_weight();
		if(ftemp!=-1f){
			et_weight.setText(String.valueOf(ftemp));
		}
		ftemp = fpv.getFipv_bmt();
		if(ftemp!=-1f){
			et_bmt.setText(String.valueOf(ftemp));
		}
		itemp = fpv.getFipv_bloodpressure_high();
		if(ftemp!=-1f){
			et_bloodpressure_high.setText(String.valueOf(itemp));
		}
		itemp = fpv.getFipv_bloodpressure_low();
		if(ftemp!=-1f){
			et_bloodpressure_low.setText(String.valueOf(itemp));
		}
		
		itemp = fpv.getFipv_has_heart_abnormal();
		switch(itemp){
		case 1:
			rgHasHeartAbnormal.check(0);
			rb_has_heart_abnormal_no.setChecked(true);
			break;
		case 2:{
			rgHasHeartAbnormal.check(1);
   			rb_has_heart_abnormal_yes.setChecked(true);
			temp = fpv.getFipv_heart_abnormal();
			et_heart_abnormal.setText(temp);
			break;
		}
		}
		itemp = fpv.getFipv_has_lung_abnormal();
		switch(itemp){
		case 1:
			rgHasLungAbnormal.check(0);
			rb_has_lung_abnormal_no.setChecked(true);
			break;
		case 2:{
			rgHasLungAbnormal.check(1);
			rb_has_lung_abnormal_yes.setChecked(true);
			temp = fpv.getFipv_lung_abnormal();
			et_lung_abnormal.setText(temp);
			break;
		}
		}
		itemp = fpv.getFipv_has_vulva_abnormal();
		switch(itemp){
		case 1:
			rgHasVulvaAbnormal.check(0);
			rb_has_vulva_abnormal_no.setChecked(true);
			break;
		case 2:{
			rgHasVulvaAbnormal.check(1);
			rb_has_vulva_abnormal_yes.setChecked(true);
			temp = fpv.getFipv_vulva_abnormal();
			et_vulva_abnormal.setText(temp);
			break;
		}
		}
		itemp = fpv.getFipv_has_vagina_abnormal();
		switch(itemp){
		case 1:
			rgHasVaginaAbnormal.check(0);
			rb_has_vagina_abnormal_no.setChecked(true);
			break;
		case 2:{
			rgHasVaginaAbnormal.check(1);
			rb_has_vagina_abnormal_yes.setChecked(true);
			temp = fpv.getFipv_vagina_abnormal();
			et_vagina_abnormal.setText(temp);
			break;
		}
		}
		itemp = fpv.getFipv_has_cervix_abnormal();
		switch(itemp){
		case 1:
			rgHasCervixAbnormal.check(0);
			rb_has_cervix_abnormal_no.setChecked(true);
			break;
		case 2:{
			rgHasCervixAbnormal.check(1);
			rb_has_cervix_abnormal_yes.setChecked(true);
			temp = fpv.getFipv_cervix_abnormal();
			et_cervix_abnormal.setText(temp);
			break;
		}
		}
		itemp = fpv.getFipv_has_uterus_abnormal();
		switch(itemp){
		case 1:
			rgHasUterusAbnormal.check(0);
			rb_has_uterus_abnormal_no.setChecked(true);
			break;
		case 2:{
			rgHasUterusAbnormal.check(1);
			rb_has_uterus_abnormal_yes.setChecked(true);
			temp = fpv.getFipv_uterus_abnormal();
			et_uterus_abnormal.setText(temp);
			break;
		}
		}
		itemp = fpv.getFipv_has_appendix_abnormal();
		switch(itemp){
		case 1:
			rgHasAppendixAbnormal.check(0);
			rb_has_appendix_abnormal_no.setChecked(true);
			break;
		case 2:{
			rgHasAppendixAbnormal.check(1);
			rb_has_appendix_abnormal_yes.setChecked(true);
			temp = fpv.getFipv_appendix_abnormal();
			et_appendix_abnormal.setText(temp);
			break;
		}
		}
		
	}
	
	/**
	 * 初始状态：横线处为空，所有选择项未选中
	 */
	private void initialState() {
		// TODO Auto-generated method stub
		et_name.setText("");
		et_id.setText("");
		et_date.setText("");
		et_preg_week.setText("");
		et_age.setText("");
		et_husband_name.setText("");
		et_husband_age.setText("");
		et_husband_phone.setText("");
		et_gravidity.setText("");
        et_vaginal_delivery_times.setText("");
        et_cesarean_section_times.setText("");
        et_last_menses_date.setText("");
        et_ycq.setText("");
        et_past_history_other.setText("");
        et_family_other.setText("");
        et_personal_other.setText("");
        et_gyneopration.setText("");
        et_miscarry.setText("");et_dying_fetus.setText("");
        et_stillbirth.setText("");
        et_neonatal_death.setText("");
        et_birth_defect.setText("");
        et_height.setText("");
        et_weight.setText("");
        et_bmt.setText("");
        et_bloodpressure_high.setText("");
        et_bloodpressure_low.setText("");
        et_heart_abnormal.setText("");
        et_lung_abnormal.setText("");
        et_vulva_abnormal.setText("");
        et_vagina_abnormal.setText("");
        et_cervix_abnormal.setText("");
        et_uterus_abnormal.setText("");
        et_appendix_abnormal.setText("");

		rb_last_menses_1.setChecked(false);
		rb_last_menses_2.setChecked(false);
		rb_gyneopration_1.setChecked(false);
		rb_gyneopration_2.setChecked(false);
		rb_has_heart_abnormal_no.setChecked(false);
		rb_has_heart_abnormal_yes.setChecked(false);
        rb_has_lung_abnormal_no.setChecked(false);
        rb_has_lung_abnormal_yes.setChecked(false);
        rb_has_vulva_abnormal_no.setChecked(false);
        rb_has_vulva_abnormal_yes.setChecked(false);
        rb_has_vagina_abnormal_no.setChecked(false);
        rb_has_vagina_abnormal_yes.setChecked(false);
        rb_has_cervix_abnormal_no.setChecked(false);
        rb_has_cervix_abnormal_yes.setChecked(false);
        rb_has_uterus_abnormal_no.setChecked(false);
        rb_has_uterus_abnormal_yes.setChecked(false);
        rb_has_appendix_abnormal_no.setChecked(false);
        rb_has_appendix_abnormal_yes.setChecked(false);
		cb_past_history_1.setChecked(false);
		cb_past_history_2.setChecked(false);
		cb_past_history_3.setChecked(false);
		cb_past_history_4.setChecked(false);
		cb_past_history_5.setChecked(false);
		cb_past_history_6.setChecked(false);
        cb_past_history_7.setChecked(false);
        cb_past_history_8.setChecked(false);
        cb_family_1.setChecked(false);
        cb_family_2.setChecked(false);
        cb_family_3.setChecked(false);
        cb_personal_1.setChecked(false);
        cb_personal_2.setChecked(false);
        cb_personal_3.setChecked(false);
        cb_personal_4.setChecked(false);
        cb_personal_5.setChecked(false);
        cb_personal_6.setChecked(false);
        cb_pregnance_history_1.setChecked(false);
        cb_pregnance_history_2.setChecked(false);
        cb_pregnance_history_3.setChecked(false);
        cb_pregnance_history_4.setChecked(false);
        cb_pregnance_history_5.setChecked(false);
		
	}

	/**
	 * 将初始化后的view添加到父view中
	 */
	public void setMyView(){
		LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		this.addView(child,lp);
	}
	
	/**
	 * 从数据库中检出数据并存到对象中
	 */
	public void fetchData(){
		ht = new HereTipa(mContext);
		ht.selectNameAndCode(this.profileid);
		
		fpv = new FirstPrenatalVisit(mContext);
		fpv.selectFirstPrenatalVisit(this.profileid);
	}
	
	/**
	 * 保存信息并更新数据库
	 */
	public void saveViews(){
		String temp="";
//		int itemp=-1;
//		float ftemp=-1f;
		StringBuffer sb=null;
		
		FirstPrenatalVisit fp = new FirstPrenatalVisit(mContext);
		fp.setFipv_id(this.profileid);
		fp.selectFirstPrenatalVisit(profileid);
		
		temp = et_date.getText().toString();
		fp.setFipv_date(temp);
		temp = et_preg_week.getText().toString();
		if(temp.length()>0){
			fp.setFipv_preg_week(Integer.parseInt(temp));
		}
		temp = et_age.getText().toString();
		if(temp.length()>0){
			fp.setFipv_age(Integer.parseInt(temp));
		}
		temp = et_husband_name.getText().toString();
		fp.setFipv_husband_name(temp);
		temp = et_husband_age.getText().toString();
		if(temp.length()>0){
			fp.setFipv_husband_age(Integer.parseInt(temp));
		}
		temp = et_husband_phone.getText().toString();
		fp.setFipv_husband_phone(temp);
		temp = et_gravidity.getText().toString();
		if(temp.length()>0){
			fp.setFipv_gravidity(Integer.parseInt(temp));
		}
		temp = et_vaginal_delivery_times.getText().toString();
		if(temp.length()>0){
			fp.setFipv_vaginal_delivery_times(Integer.parseInt(temp));
		}
		temp = et_cesarean_section_times.getText().toString();
		if(temp.length()>0){
			fp.setFipv_cesarean_section_times(Integer.parseInt(temp));
		}
		
		if(rb_last_menses_1.isChecked()==true){
			fp.setFipv_last_menses(1);
			temp = et_last_menses_date.getText().toString();
			fp.setFipv_last_menses_date(temp);
		}else if(rb_last_menses_2.isChecked()==true){
			fp.setFipv_last_menses(2);
		}
		temp = et_ycq.getText().toString();
		fp.setFipv_ycq(temp);
		
		sb = new StringBuffer();
		if(cb_past_history_1.isChecked()==true){
			sb.append("1,");
		}
		if(cb_past_history_2.isChecked()==true){
			sb.append("2,");
		}
		if(cb_past_history_3.isChecked()==true){
			sb.append("3,");
		}
		if(cb_past_history_4.isChecked()==true){
			sb.append("4,");
		}
		if(cb_past_history_5.isChecked()==true){
			sb.append("5,");
		}
		if(cb_past_history_6.isChecked()==true){
			sb.append("6,");
		}
		if(cb_past_history_7.isChecked()==true){
			sb.append("7,");
		}
		if(cb_past_history_8.isChecked()==true){
			sb.append("8,");
			temp = et_past_history_other.getText().toString();
			fp.setFipv_past_history_other(temp);
		}
		fp.setFipv_past_history(sb.toString());
		
		sb = new StringBuffer();
		if(cb_family_1.isChecked()==true){
			sb.append("1,");
		}
		if(cb_family_2.isChecked()==true){
			sb.append("2,");
		}
		if(cb_family_3.isChecked()==true){
			sb.append("3,");
			temp = et_family_other.getText().toString();
			fp.setFipv_family_other(temp);
		}
		fp.setFipv_family(sb.toString());
		
		sb = new StringBuffer();
		if(cb_personal_1.isChecked()==true){
			sb.append("1,");
		}
		if(cb_personal_2.isChecked()==true){
			sb.append("2,");
		}
		if(cb_personal_3.isChecked()==true){
			sb.append("3,");
		}
		if(cb_personal_4.isChecked()==true){
			sb.append("4,");
		}
		if(cb_personal_5.isChecked()==true){
			sb.append("5,");
		}
		if(cb_personal_6.isChecked()==true){
			sb.append("6,");
			temp = et_personal_other.getText().toString();
			fp.setFipv_personal_other(temp);
		}
		fp.setFipv_personal(sb.toString());
		
		if(rb_gyneopration_1.isChecked()==true){
			fp.setFipv_has_gyneopration(1);
		}else if(rb_gyneopration_2.isChecked()==true){
			fp.setFipv_has_gyneopration(2);
			temp = et_gyneopration.getText().toString();
			fp.setFipv_gyneopration(temp);
		}
		
		sb = new StringBuffer();
		if(cb_pregnance_history_1.isChecked()==true){
			sb.append("1,");
			temp = et_miscarry.getText().toString();
			if(temp.length()>0){
				fp.setFipv_miscarry(Integer.parseInt(temp));
			}
		}
		if(cb_pregnance_history_2.isChecked()==true){
			sb.append("2,");
			temp = et_dying_fetus.getText().toString();
			if(temp.length()>0){
				fp.setFipv_dying_fetus(Integer.parseInt(temp));
			}
		}
		if(cb_pregnance_history_3.isChecked()==true){
			sb.append("3,");
			temp = et_stillbirth.getText().toString();
			if(temp.length()>0){
				fp.setFipv_stillbirth(Integer.parseInt(temp));
			}
		}
		if(cb_pregnance_history_4.isChecked()==true){
			sb.append("4,");
			temp = et_neonatal_death.getText().toString();
			if(temp.length()>0){
				fp.setFipv_neonatal_death(Integer.parseInt(temp));
			}
		}
		if(cb_pregnance_history_5.isChecked()==true){
			sb.append("5,");
			temp = et_birth_defect.getText().toString();
			if(temp.length()>0){
				fp.setFipv_birth_defect(Integer.parseInt(temp));
			}
		}
		fp.setFipv_pregnance_history(sb.toString());
		
		temp = et_height.getText().toString();
		if(temp.length()>0){
			fp.setFipv_height(Integer.parseInt(temp));
		}
		temp = et_weight.getText().toString();
		if(temp.length()>0){
			fp.setFipv_weight(Float.valueOf(temp));
		}
		temp = et_bmt.getText().toString();
		if(temp.length()>0){
			fp.setFipv_bmt(Float.valueOf(temp));
		}
		temp = et_bloodpressure_high.getText().toString();
		if(temp.length()>0){
			fp.setFipv_bloodpressure_high(Integer.parseInt(temp));
		}
		temp = et_bloodpressure_low.getText().toString();
		if(temp.length()>0){
			fp.setFipv_bloodpressure_low(Integer.parseInt(temp));
		}
		
		if(rb_has_heart_abnormal_no.isChecked()==true){
			fp.setFipv_has_heart_abnormal(1);
		}else if(rb_has_heart_abnormal_yes.isChecked()==true){
			fp.setFipv_has_heart_abnormal(2);
			temp = et_heart_abnormal.getText().toString();
			fp.setFipv_heart_abnormal(temp);
		}
		if(rb_has_lung_abnormal_no.isChecked()==true){
			fp.setFipv_has_lung_abnormal(1);
		}else if(rb_has_lung_abnormal_yes.isChecked()==true){
			fp.setFipv_has_lung_abnormal(2);
			temp = et_lung_abnormal.getText().toString();
			fp.setFipv_lung_abnormal(temp);
		}
		if(rb_has_vulva_abnormal_no.isChecked()==true){
			fp.setFipv_has_vulva_abnormal(1);
		}else if(rb_has_vulva_abnormal_yes.isChecked()==true){
			fp.setFipv_has_vulva_abnormal(2);
			temp = et_vulva_abnormal.getText().toString();
			fp.setFipv_vulva_abnormal(temp);
		}
		if(rb_has_vagina_abnormal_no.isChecked()==true){
			fp.setFipv_has_vagina_abnormal(1);
		}else if(rb_has_vagina_abnormal_yes.isChecked()==true){
			fp.setFipv_has_vagina_abnormal(2);
			temp = et_vagina_abnormal.getText().toString();
			fp.setFipv_vagina_abnormal(temp);
		}
		if(rb_has_cervix_abnormal_no.isChecked()==true){
			fp.setFipv_has_cervix_abnormal(1);
		}else if(rb_has_cervix_abnormal_yes.isChecked()==true){
			fp.setFipv_has_cervix_abnormal(2);
			temp = et_cervix_abnormal.getText().toString();
			fp.setFipv_cervix_abnormal(temp);
		}
		if(rb_has_uterus_abnormal_no.isChecked()==true){
			fp.setFipv_has_uterus_abnormal(1);
		}else if(rb_has_uterus_abnormal_yes.isChecked()==true){
			fp.setFipv_has_uterus_abnormal(2);
			temp = et_uterus_abnormal.getText().toString();
			fp.setFipv_uterus_abnormal(temp);
		}
		if(rb_has_appendix_abnormal_no.isChecked()==true){
			fp.setFipv_has_appendix_abnormal(1);
		}else if(rb_has_appendix_abnormal_yes.isChecked()==true){
			fp.setFipv_has_appendix_abnormal(2);
			temp = et_appendix_abnormal.getText().toString();
			fp.setFipv_appendix_abnormal(temp);
		}
		
		fp.updateFirstPrenatalVisit(this.profileid);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v==et_date){
			mPopupWindow = new MyPopupWindow(mContext);			
			mPopupWindow.showAtLocation(v.findViewById(R.id.first_date), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_date.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}
				
			});
		}
		else if(v==et_last_menses_date){
			rb_last_menses_1.setChecked(true);
			rb_last_menses_2.setChecked(false);
			
			mPopupWindow = new MyPopupWindow(mContext);			
			mPopupWindow.showAtLocation(v.findViewById(R.id.first_last_menses_date), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_last_menses_date.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}
				
			});
		}
		else if(v==rb_last_menses_1){
			rb_last_menses_1.setChecked(true);
			rb_last_menses_2.setChecked(false);
		}
		else if(v==rb_last_menses_2){
			rb_last_menses_1.setChecked(false);
			rb_last_menses_2.setChecked(true);
		}
		else if(v==et_past_history_other){
			cb_past_history_8.setChecked(true);
		}
		else if(v==et_family_other){
			cb_family_3.setChecked(true);
		}
		else if(v==et_personal_other){
			cb_personal_6.setChecked(true);
		}
		else if(v==et_gyneopration){
			rb_gyneopration_1.setChecked(false);
			rb_gyneopration_2.setChecked(true);
		}
		else if(v==et_miscarry){
			cb_pregnance_history_1.setChecked(true);
		}
		else if(v==et_dying_fetus){
			cb_pregnance_history_2.setChecked(true);
		}
		else if(v==et_stillbirth){
			cb_pregnance_history_3.setChecked(true);
		}
		else if(v==et_neonatal_death){
			cb_pregnance_history_4.setChecked(true);
		}
		else if(v==et_birth_defect){
			cb_pregnance_history_5.setChecked(true);
		}
		else if(v==et_heart_abnormal){
			rb_has_heart_abnormal_yes.setChecked(true);
			rb_has_heart_abnormal_no.setChecked(false);
		}
		else if(v==et_lung_abnormal){
			rb_has_lung_abnormal_yes.setChecked(true);
			rb_has_lung_abnormal_no.setChecked(false);
		}
		else if(v==et_vulva_abnormal){
			rb_has_vulva_abnormal_yes.setChecked(true);
			rb_has_vulva_abnormal_no.setChecked(false);
		}
		else if(v==et_vagina_abnormal){
			rb_has_vagina_abnormal_yes.setChecked(true);
			rb_has_vagina_abnormal_no.setChecked(false);
		}
		else if(v==et_cervix_abnormal){
			rb_has_cervix_abnormal_yes.setChecked(true);
			rb_has_cervix_abnormal_no.setChecked(false);
		}
		else if(v==et_uterus_abnormal){
			rb_has_uterus_abnormal_yes.setChecked(true);
			rb_has_uterus_abnormal_no.setChecked(false);
		}
		else if(v==et_appendix_abnormal){
			rb_has_appendix_abnormal_yes.setChecked(true);
			rb_has_appendix_abnormal_no.setChecked(false);
		}
		
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		// TODO Auto-generated method stub
		if(v==et_past_history_other&&hasFocus){
			cb_past_history_8.setChecked(true);
		}
		else if(v==et_family_other&&hasFocus){
			cb_family_3.setChecked(true);
		}
		else if(v==et_personal_other&&hasFocus){
			cb_personal_6.setChecked(true);
		}
		else if(v==et_gyneopration&&hasFocus){
			rb_gyneopration_1.setChecked(false);
			rb_gyneopration_2.setChecked(true);
		}
		else if(v==et_miscarry&&hasFocus){
			cb_pregnance_history_1.setChecked(true);
		}
		else if(v==et_dying_fetus&&hasFocus){
			cb_pregnance_history_2.setChecked(true);
		}
		else if(v==et_stillbirth&&hasFocus){
			cb_pregnance_history_3.setChecked(true);
		}
		else if(v==et_neonatal_death&&hasFocus){
			cb_pregnance_history_4.setChecked(true);
		}
		else if(v==et_birth_defect&&hasFocus){
			cb_pregnance_history_5.setChecked(true);
		}
		else if(v==et_heart_abnormal&&hasFocus){
			rb_has_heart_abnormal_yes.setChecked(true);
			rb_has_heart_abnormal_no.setChecked(false);
		}
		else if(v==et_lung_abnormal&&hasFocus){
			rb_has_lung_abnormal_yes.setChecked(true);
			rb_has_lung_abnormal_no.setChecked(false);
		}
		else if(v==et_vulva_abnormal&&hasFocus){
			rb_has_vulva_abnormal_yes.setChecked(true);
			rb_has_vulva_abnormal_no.setChecked(false);
		}
		else if(v==et_vagina_abnormal&&hasFocus){
			rb_has_vagina_abnormal_yes.setChecked(true);
			rb_has_vagina_abnormal_no.setChecked(false);
		}
		else if(v==et_cervix_abnormal&&hasFocus){
			rb_has_cervix_abnormal_yes.setChecked(true);
			rb_has_cervix_abnormal_no.setChecked(false);
		}
		else if(v==et_uterus_abnormal&&hasFocus){
			rb_has_uterus_abnormal_yes.setChecked(true);
			rb_has_uterus_abnormal_no.setChecked(false);
		}
		else if(v==et_appendix_abnormal&&hasFocus){
			rb_has_appendix_abnormal_yes.setChecked(true);
			rb_has_appendix_abnormal_no.setChecked(false);
		}
		else if(v==et_date&&hasFocus){
			mPopupWindow = new MyPopupWindow(getContext());			
			mPopupWindow.showAtLocation(v.findViewById(R.id.first_date  ), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_date.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}
				
			});
		}
	}
}
